//
//  xxViewController.m
//  tcpoctest
//
//  Created by gump on 10/22/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "xxViewController.h"
#import "ViewManager.h"
#import "WFLabel_blackShadow.h"
#import "systemCell.h"
#import "XMMessageInfo.h"
#import "DataControl.h"
#import "requestCheckInfo.h"
#import "messagedb.h"
#import "chatuserdb.h"
#import "chatDataSource.h"
#import "MessageDataSource.h"
#import "ChatContentInfo.h"

@implementation xxViewController

-(id)init
{
    self = [super init];
    
    if(self)
    {
    }
    
    return self;
}

-(void)initUI
{
    self.vctype = VI_XX;
    
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"消息";
    
    self.mtablev =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 66)
                                         style:UITableViewStylePlain];
    self.mtablev.backgroundColor=CELL_BG_COLOR;
    self.mtablev.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    self.mtablev.delegate =self;
    self.mtablev.dataSource =self;
    [self.view insertSubview:self.mtablev belowSubview:self.mtitle];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        return [[DataControl shareControl].messageDS GetMagArrCount];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row=[indexPath row];
    static NSString * CellIdentifier = @"Cell";
    __autoreleasing systemCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[systemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    switch (section)
    {
        case 0:
        {
            XMMessageInfo * xi = [[DataControl shareControl].messageDS getMsgArrObject:row];
            if(xi)
            {
                cell.mtimeLabel.text = [[[DataControl shareControl].chatDS getLastChatInfoByMail:xi.strmail] getLastContentInfoTime];//时间
                
                //聊天内容的字体颜色和大小
                    if (xi.mbAlreadayArgee) {
                        cell.mpromptLabel.textColor = [UIColor grayColor];//语音已读变为灰色
                    }else{
                        cell.mpromptLabel.textColor = [UIColor colorWithRed:180/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];//语音未读为红色
                    }
                
                if (xi.badgeNum > 0) {
                    [cell ShowBadge:xi.badgeNum];
                }
                else
                {
                    [cell HideBadge];
                }
                
                
                //判断消息类型
                switch (xi.messageType) {
                    case XMMT_CHAT:
                    {
                        FirendInfo * fi = [[DataControl shareControl] SearchInfo:xi.strmail];//读取用户信息
                        ChatContentInfo *lastcinfo = [[DataControl shareControl].chatDS getLastChatInfoByMail:xi.strmail];//取得最后一条聊天信息
                
                        cell.mtitleLabel.text = fi.name;//昵称
                         NSString *url = [[DataControl shareControl] SearchHeadImage:xi.strmail];
                        if (fi != nil)
                        {
                            if ([url length] > 0)
                            {
                                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                                UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                                if (headImage) {
                                    cell.imageView.image = nil;
                                    cell.mimageview.image = [UIImage imageWithContentsOfFile:url];
                                }
                            }else{
                                cell.mimageview.image = nil;
                                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                            }
                            //显示最后一条聊天信息
                            if (lastcinfo != nil) {
                                switch (lastcinfo.chatType) {
                                    case CT_TEXT:
                                        case CT_TELEPATHY:
                                    {
                                        cell.mpromptLabel.text = lastcinfo.strtext;
                                    }
                                        break;
                                        
                                    case CT_IMAGE:
                                    {
                                        cell.mpromptLabel.text = @"[图片]";
                                    }
                                        break;
                                        
                                    case CT_VOICE:
                                    {
                                        cell.mpromptLabel.text = @"[语音]";
                                    }
                                        break;
                                    case CT_GIFT:
                                    {
                                        cell.mpromptLabel.text = @"[礼物]";
                                    }
                                        break;
                                    default:
                                        break;
                                }

                            }
                        
                        }
                    }
                        break;
                    case XMMT_BECARE:
                    {
                        FirendInfo *fi = [[DataControl shareControl] SearchInfo: xi.strmail];
                        NSString *url = [[DataControl shareControl] SearchHeadImage:xi.strmail];
                        if (fi != nil)
                        {
                            cell.mtitleLabel.text = fi.name;
                            
                            if ([url length] > 0)
                            {
                                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                                UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                                if (headImage) {
                                    cell.imageView.image = nil;
                                    cell.mimageview.image = [UIImage imageWithContentsOfFile:url];
                                }
                            }else{
                                cell.mimageview.image = nil;
                                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                            }
                            cell.mpromptLabel.text = [NSString stringWithFormat:@"你被%@关注了",fi.name];
                        }
                    }
                        break;
                    default:
                        break;
                }
            }
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch ([indexPath section]) {
        case 0:
        {
            XMMessageInfo *xi = [[DataControl shareControl].messageDS getMsgArrObject:[indexPath row]];
            if (xi == nil) {
                return;
            }
            
            switch (xi.messageType)
            {
                case XMMT_CHAT:
                {
                    if (!xi.mbAlreadayArgee) {
                        
                        [DataControl shareControl].mBadgeCount -= xi.badgeNum;
                        [[DataControl shareControl].UICommendDS addUICommand:UC_SETBADGE];
                        
                        xi.badgeNum = 0;
                        xi.mbAlreadayArgee = true;
                        
                        [[DataBaseCtrol shareControl].messagedatabase updateMessage:xi];
                        
                    }
                    
                    [DataControl shareControl].currentFI = [[DataControl shareControl] SearchInfo:xi.strmail];
                    [DataControl shareControl].chatDS.currentChatArr = [[DataControl shareControl].chatDS GetArrByKey:xi.strmail];
                    
                    [[ViewManager sharedViewManager] showView:VI_CHAT
                                             withIsRemoveCurV:false
                                                withDirection:VD_RIGHT];
                }
                    break;
                    case XMMT_BECARE:
                {
                    if (!xi.mbAlreadayArgee) {
                        
                        [[ViewManager sharedViewManager] subXMDabge];
                        xi.mbAlreadayArgee = true;
                        
                        [[DataBaseCtrol shareControl].messagedatabase updateMessage:xi];
                        
                    }
                    
                    [DataControl shareControl].currentFI = [[DataControl shareControl] SearchInfo: xi.strmail];
                    
                    [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                             withIsRemoveCurV:false
                                                withDirection:VD_RIGHT];
                }
                    break;
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
    
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        
        XMMessageInfo *xi = [[DataControl shareControl].messageDS getMsgArrObject:[indexPath row]];
        if(xi != nil)
        {
            switch (xi.messageType)
            {
                case XMMT_CHAT:
                {
                    [[DataBaseCtrol shareControl].chatuserdatabase deleteChatDic:xi.strmail withIsGroup:false];
                    
                    NSMutableArray *chatarr = [[DataControl shareControl].chatDS GetArrByKey:xi.strmail];
                    if (chatarr != nil) {
                        [chatarr removeAllObjects];
                    }
                    
                    
                    [[DataControl shareControl].chatDS RemoveObjectByKey:xi.strmail];
                }
                    break;
                case XMMT_BECARE:
                {
                }
                    break;
                default:
                    break;
            }
            
            if (!xi.mbAlreadayArgee) 
            {
                [[ViewManager sharedViewManager] subXMDabge];
            }
            
            [[MessageManager sharedManager] DeleteMessage:xi];
        }
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
    }
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark -private fun-
-(void)refreshView
{
    [[DataControl shareControl].messageDS MsgSortArr];
    
    [self.mtablev reloadData];
}

-(void)viewWillShow
{
    [[DataControl shareControl].messageDS MsgSortArr];
    
    [self.mtablev reloadData];
}

@end
